package com.hstcs.ui;

import static com.badlogic.gdx.scenes.scene2d.actions.Actions.moveTo;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.run;
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.sequence;

import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.hstcs.assets.Assets;
import com.hstcs.assets.Config;
import com.hstcs.assets.FileName;
import com.hstcs.game.XGame;
import com.hstcs.screen.GameScreen;



public class HSelect extends Table {
//	private Image scroll;
	private Table tableTop, tableBottom;
	private int figure  = Config.GUNNER;
	private  Button left;
	private Button right;
	private Image figureImage;
	public HSelect(){
	//	Texture figuresTexture = Assets.get(FileName._MAIN_BG);
		//scroll = new Image(Assets.get(FileName._SCROLL));
		this.setFillParent(true);
		tableTop 	= new Table();
		tableBottom = new Table();
		tableTop.setBackground(new TextureRegionDrawable(Assets.getRegion(FileName._SCROLL)));
		//figureImage = new Image();
		loadFigure();

		this.add(tableTop);
		this.row();
		this.add(tableBottom);
		left = new Button(new TextureRegionDrawable(Assets.getRegions(FileName._LEFT_ARROW, 3)[0]),
				new TextureRegionDrawable(Assets.getRegions(FileName._LEFT_ARROW, 3)[1]));
		right = new Button(new TextureRegionDrawable(Assets.getRegions(FileName._RIGHT_ARROW, 3)[0]),
				new TextureRegionDrawable(Assets.getRegions(FileName._RIGHT_ARROW, 3)[1]));
		tableBottom.add(left).expandX().left();
		tableBottom.add(right).expandX().right();
		tableBottom.setTouchable(Touchable.enabled);
		
	//	this.debugAll();
		listen();				
	}
	
	public void loadFigure()
	{			
		tableBottom.setBackground( new TextureRegionDrawable(Assets.getRegions( FileName._HERO_FIGURES, 4)[figure]));
		//figureImage.setDrawable(new TextureRegionDrawable(Assets.getRegions( FileName._HERO_FIGURES, 4)[figure]));
	}
	
	public void listen()
	{
		left.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				if(figure > Config.GUNNER)
				{
					figure -= 1;
					loadFigure();
				}			
			}
		});
		
		right.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				if(figure < Config.PRIESTESS)
				{
					figure += 1;
					loadFigure();
				}			
			}
		});
		tableBottom.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);				
				if(x > left.getWidth() && x < tableBottom.getWidth() - right.getWidth()){
					tableBottom.clear();
					Label label = new Label("READY", Assets.skin);
					tableBottom.add(label);
					label.addAction(sequence(Actions.color(Color.BLUE, 1f) ,run(new Runnable() {
					    public void run () {
					    	XGame.getInstance().setScreen(GameScreen.getInstance());
					    }    
					})));
					 Sound  sound = Assets.get(FileName._SOUND_LIST[0]);
					 sound.play();
				
				}				
			}
		});
		
	}
}
